package com.baizhi.controller;


import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin(origins = "*",allowCredentials = "true")
//@Controller
//@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

//    @ResponseBody
//    @RequestMapping("/show")
//    @RequestMapping(value="/users",method=RequestMethod.GET)
    @GetMapping(value = "/users")
    public Map show(@RequestParam(defaultValue = "1") Integer pageNum,
                    @RequestParam(defaultValue = "5")Integer pageSize,
                    @RequestParam(defaultValue = "") String query){
        System.out.println("pageNum = " + pageNum);
        System.out.println("query = " + query);
        List<User> list = userService.selectAll(pageNum, pageSize, query);
        PageInfo<User> pageInfo = new PageInfo<>(list, 10);
        long total = pageInfo.getTotal();
        int[] pages = pageInfo.getNavigatepageNums();

        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("pages",pages);
        map.put("total",total);
        return map;
    }


//    @ResponseBody
//    @RequestMapping("/remove")
    @DeleteMapping(value = "/users/{id}")
    public Map remove(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        HashMap<String, Object> map = new HashMap<>();
        try {
            userService.deleteById(id);
            map.put("status","success");
        } catch (Exception e) {
            map.put("status","failed");
            e.printStackTrace();
        }
        return map;
    }

//    @ResponseBody
//    @RequestMapping("/showOne")
    @GetMapping(value = "/users/{id}")
    public Map showOne(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        HashMap<String, Object> map = new HashMap<>();
        try {
            User user = userService.selectById(id);
            map.put("status","success");
            map.put("user",user);
        } catch (Exception e) {
            map.put("status","failed");
            e.printStackTrace();
        }
        return map;
    }
//    @ResponseBody
//    @RequestMapping("/updateOne")
    @PutMapping(value = "/users")
    public Map updateOne(@RequestBody User user){
        HashMap<String, Object> map = new HashMap<>();
        try {
            userService.updateUser(user);
            map.put("status","success");
        } catch (Exception e) {
            map.put("status","failed");
            e.printStackTrace();
        }
        return map;
    }
//    @ResponseBody
//    @RequestMapping("/addOne")
    @PostMapping(value = "/users")
    public Map addOne(@RequestBody User user){
        HashMap<String, Object> map = new HashMap<>();
        try {
            userService.insertUser(user);
            map.put("status","success");
        } catch (Exception e) {
            map.put("status","failed");
            e.printStackTrace();
        }
        return map;
    }
}
